<!--
* description: 备件主库位-新增编辑弹窗
-->
<template>
  <section class="editCarBrand">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="400px"
      @close="sendCode"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row :gutter="26">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :validrule="comp.validrule"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :span="comp.span || 8"
            :isMul="comp.isMul"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :options="comp.options"
            :dateOptionsType="comp.dateOptionsType"
            :WAREHOUSE_ID="formField.WAREHOUSE_ID"
            :AREA_CODE="formField.AREA_CODE"
            :isInput="comp.isInput"
            :dataToObject="comp.dataToObject"
          ></component>
          <!--             @changeCallBack="warehouseChange"
          :isCallBack="comp.isCallBack"-->
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/wms/mixins/formMixins";
import { wmsApis } from "@/api/wms/baseData/api";
import storage from "@/utils/storage";
import request from "@/utils/request";
export default {
  // 组件混入对象
  mixins: [formMixins],
  components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  watch: {
    popupsVisible(val) {
      if (val) {
        if (
          this.formField.pageId !== "" &&
          this.$route.query.pageId !== undefined &&
          this.$route.query.pageId !== this.formField.pageId
        ) {
          this.formField.pageId = this.$route.query.pageId;
          this.formField.pageName = this.$route.query.pageName;
        }
        this.formField.langLabelName = this.formField.label;
        this.backFormField = JSON.parse(JSON.stringify(this.formField));
      }
    }
  },
  data() {
    return {
      // 保存API配置对象
      apiConfig: wmsApis.savePartPlace,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存" //"保存"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "edit_compKey1",
          labelName: "仓库名称", //仓库名称
          codeField: "WAREHOUSE_ID",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: [],
          isRequire: true,
          changed: val => {
            this.tableComponents[2].options = [];
            this.formField.AREA_CODE = "";
            this.formField.PLACE_CODE = "";
            if (!val) {
              return;
            }
            request({
              url: "/mp/wm/vl/basedata/boxcommon/queryarea.do",
              method: "post",
              data: {
                AREA_TYPE: "00",
                WAREHOUSE_IDS: val
              }
            }).then(res => {
              if (res.listRow && res.listRow.length > 0) {
                res.listRow.map(item => {
                  this.tableComponents[2].options.push({
                    label: item.AREA_NAME + "-" + item.AREA_ATTR_NAME,
                    value: item.AREA_CODE
                  });
                });
              }
            });
          }
        },
        {
          compKey: "edit_compKey2",
          labelName: "备件名称",
          textField: "PART_NAME",
          codeField: "PART_NO",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsPartInfo/indexB"),
          type: "propus",
          isMust: true,
          span: 23,
          isMul: false,
          isRequire: true,
          returnCodeField: "PART_NO",
          returnTextField: "PART_NAME"
        },
        {
          compKey: "edit_compKey3",
          labelName: "库区",
          codeField: "AREA_CODE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          span: 23,
          isMust: true,
          isMul: false,
          options: [],
          isRequire: true,
          changed: val => {
            this.formField.PLACE_CODE = "";
          }
        },
        {
          compKey: "edit_compKey4",
          labelName: "库位",
          textField: "PLACE_CODE",
          codeField: "PLACE_CODE",
          component: () => import("@/components/wms/wmsEjectWindows/wmsPlace"),
          type: "propus",
          isMust: true,
          span: 23,
          isMul: false,
          isRequire: true,
          returnCodeField: "PLACE_CODE",
          returnTextField: "PLACE_CODE"
        },
        {
          compKey: "edit_compKey5",
          labelName: "最大存放量",
          span: 23,
          codeField: "MAX_NUM",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            if (this.formField.MAX_NUM < 1) {
              this.$refs["edit_compKey5"][0].modelCode = "";
              this.formField.MAX_NUM = "";
            }
            this.$refs["edit_compKey5"][0].modelCode = (
              this.formField.MAX_NUM + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.MAX_NUM = (this.formField.MAX_NUM + "")
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 5
          }
        },
        {
          compKey: "edit_compKey6",
          labelName: "补货阈值",
          span: 23,
          codeField: "REPLENISHMENT_NUM",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          isInput: true,
          changed: val => {
            if (this.formField.REPLENISHMENT_NUM < 1) {
              this.$refs["edit_compKey6"][0].modelCode = "";
              this.formField.REPLENISHMENT_NUM = "";
            }
            this.$refs["edit_compKey6"][0].modelCode = (
              this.formField.REPLENISHMENT_NUM + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
            this.formField.REPLENISHMENT_NUM = (
              this.formField.REPLENISHMENT_NUM + ""
            )
              .replace(/[^\d^\.]+/g, "")
              .replace(".", "$#$")
              .replace(/\./g, "")
              .replace("$#$", ".");
          },
          dataToObject: {
            maxLength: 5
          }
        }
      ],
      // 标题
      textMap: {
        edit: "编辑备件主库位设置",
        add: "新增备件主库位设置"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        // 仓库
        WAREHOUSE_ID: "",
        // 备件
        PART_NO: "",
        PART_NAME: "",
        // 库位
        PLACE_CODE: "",
        // 库区
        AREA_CODE: "",
        // 最大存放量
        MAX_NUM: "",
        // 阈值
        REPLENISHMENT_NUM: "",
        // 组件ID
        PART_PLACE_ID: ""
      }
    };
  },
  methods: {
    onCreated() {
      if (this.curPopupsVisible) {
        let WAREHOUSE_ID = this.formField.WAREHOUSE_ID;
        this.formField.WAREHOUSE_ID = "";
        request({
          url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
          method: "post",
          data: {
            IS_ENABLE: "1"
          }
        }).then(res => {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_ID = WAREHOUSE_ID;
        });

        if (this.popupsState === "edit") {
          let AREA_CODE = this.formField.AREA_CODE;
          this.formField.AREA_CODE = "";
          request({
            url: "/mp/wm/vl/basedata/boxcommon/queryarea.do",
            method: "post",
            data: {
              AREA_TYPE: "00",
              WAREHOUSE_IDS: WAREHOUSE_ID
            }
          }).then(res => {
            if (res.listRow && res.listRow.length > 0) {
              res.listRow.map(item => {
                this.tableComponents[2].options.push({
                  label: item.AREA_NAME + "-" + item.AREA_ATTR_NAME,
                  value: item.AREA_CODE
                });
              });
              this.formField.AREA_CODE = AREA_CODE;
            }
          });
        }
      }
    },
    //保存
    save() {
      this.$utils.validataMoth(this, "validpopup");
      if (this.valid) {
        var saveObj = JSON.parse(JSON.stringify(this.formField));
        //保存表单
        this.saveForm(saveObj);
        storage.remove(this.$route.query.pageCode);
      }
    }
  }
};
</script>
<style type="text/css" scoped>
.filter-params .el-col label .curName {
  width: 150px;
}
</style>